package com.xj.api.bussiness.userInfo.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.userInfo.service.ITbUserInfoService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.userInfo.entity.TbUserInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Hmemb
 * @since 2018-03-16
 */
@RestController
@RequestMapping("userInfo")
public class TbUserInfoController extends BaseController{
	
	@Autowired
	private ITbUserInfoService userInfoService;
	
	@GetMapping
	@ApiOperation(value = "  列表", httpMethod = "GET", notes = "  列表")
	public AbstractBean userInfoList(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize){
		return json(userInfoService.selectMapsPage(new Page<TbUserInfo>(pageNum, pageSize), new EntityWrapper<TbUserInfo>()));
	}
	
	@GetMapping("/{id}.auth")
	@ApiOperation(value = "  詳情", httpMethod = "GET", notes = "  詳情")
	public AbstractBean userInfoContent(@PathVariable Integer id){
		return json(userInfoService.selectById(id));
	}
	
	@PostMapping("save")
	@ApiOperation(value = "  新增", httpMethod = "POST", notes = " 新增")
	public AbstractBean add(TbUserInfo tbuserinfo){
		if(userInfoService.getUserInfoByCondition(tbuserinfo).size()> 0){
			return error();
		}else {
			if(!userInfoService.insertOrUpdate(tbuserinfo)){
				return error();
			}
			return success();
		}
	}
	@DeleteMapping("{id}/delete")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!userInfoService.deleteById(id)){
		return error();
		}
		return success();
    }
	@PostMapping("{identification}/getUserInfoByCondition")
	@ApiOperation(value = " 查询", httpMethod = "POST", notes = " 查询")
	public AbstractBean getUserInfoByCondition(@PathVariable(required = true) String identification){
		if(!identification.equals("") && identification !=null){
			TbUserInfo userInfo = new TbUserInfo();
			userInfo.setIdentification(identification);
			List<TbUserInfo>  list=userInfoService.getUserInfoByCondition(userInfo);
			if(list!= null && list.size()>0){
				return json(list);
			}else{
				return error();
			}
		}else{
			return error();
		}
	}

	@PostMapping("{password}/getUserInfoByPassword,{identification}/getUserInfoByPassword")
	@ApiOperation(value = " 查询", httpMethod = "POST", notes = " 查询")
	public AbstractBean getUserInfoByPassword(@PathVariable(required = true) String password,@PathVariable(required = true) String identification){
		TbUserInfo userInfo = new TbUserInfo();
		userInfo.setPassword(password);
		userInfo.setIdentification(identification);
		List<TbUserInfo> list=userInfoService.getUserInfoByCondition(userInfo);
		if(!password.equals("") && password !=null && !identification.equals("") && identification !=null){
			if(list!= null && list.size()>0){
				return json(list);
			}else{
				return error();
			}
		}else{
			return error();
		}
	}
	@PostMapping("{identification}/getUserStatusByCondition")
	@ApiOperation(value = " 身份证查询", httpMethod = "POST", notes = " 身份证查询")
	public AbstractBean getUserStatusByCondition(@PathVariable(required = true) String identification){
		if(!identification.equals("") && identification !=null){
			TbUserInfo userInfo = new TbUserInfo();
			userInfo.setIdentification(identification);
			List<TbUserInfo>  list=userInfoService.getUserInfoByCondition(userInfo);
			if(list!= null && list.size()>0){
				return SuccessMessage("用户存在","1");
			}else{
				return fail("用户不存在","0");
			}
		}else{
			return error();
		}
	}

	@GetMapping("/{identification}/selectWxUserByIdentification")
	@ApiOperation(value = "根据身份证获取用户的微信信息", httpMethod = "GET", notes = "  根据身份证获取用户的微信信息")
	public AbstractBean selectWxUserByIdentification(@PathVariable(required = true) String identification){
		return json(userInfoService.selectWxUserByIdentification(identification));
	}
}
